<?php
/**
 * Create by 11li
 * Date 2020/1/16
 * Time 8:46
 *
 */

namespace app\api\controller\v3;


use app\admin\model\User;
use app\common\controller\Api;
use app\common\model\AgentChain;
use app\common\model\AgentSendTips;
use app\common\model\AgentShop;
use app\common\model\Commission;
use app\common\service\AgentService;
use app\common\service\CommissionService;
use app\common\service\ShipOrderService;
use app\common\service\UserService;
use think\Db;

class Warehouse extends Api
{
	
	/**
	 * 统计
	 */
	public function statistics() {
		$userId = $this->auth->id;
		$agentId = UserService::getUserAgentId($userId);
		if(!$agentId) {
			$this->error('权限不足');
		}
		
		$agent = AgentShop::get($agentId);
		if($agent['role'] != User::LEVEL_WAREHOUSE) {
			$this->error('权限不足');
		}
		
		$subAgent = AgentService::getAgentSubIds($agentId, 'warehouse_id');
		//代理ID集合
		$subAgentIds = array_merge([$agentId], $subAgent);
		//代理用户ID集合
		$subAgentUserIds = array_merge([$userId], array_keys($subAgent));
		
		//代言人ID集合
		$promotionIds = AgentService::getAgentSubPromotionIds($subAgentIds);
		//代言人用户ID集合
		$promotionUserIds = array_keys($promotionIds);
		
		$userIds = array_merge($subAgentUserIds, $promotionUserIds);
		//粉丝
		$fansNumber = UserService::getFansNumberByUserIds($userIds);
		
		//场景店
		$shop = Db::name('agentShop')
			->where(['id' => ['in', $subAgentIds], 'status' => AgentShop::STATUS_Y, 'role' => User::LEVEL_SHOP])
			->count();
		
		//提货点
		$warehouse = Db::name('agentShop')
			->where(['id' => ['in', $subAgentIds], 'status' => AgentShop::STATUS_Y, 'role' => User::LEVEL_WAREHOUSE])
			->count();
		
		//推广员
		$promotionNumber = count($promotionIds);
		
		//库存
		$sotckNumber = AgentService::getStockNumberByAgentId($agentId);
		
		$commissionAmount = CommissionService::getCommissionGroupTypeByUserId($userId);
		
		//销售佣金
		$saleCommission = $commissionAmount[Commission::TYPE_SALE] ??0;
		
		//推荐佣金
		$recommendCommission = $commissionAmount[Commission::TYPE_RECOMMEND] ?? 0;
		
		//提货佣金
		$destoryCommission = $commissionAmount[Commission::TYPE_GOODS] ?? 0;
		//铺货佣金
		$stockCommission = $commissionAmount[Commission::TYPE_STOCK] ??0;
		
		//待签收数量
		$waitSignNumber = ShipOrderService::getWaitSignNumberByAgentId($agentId);
		
		//我的奖励数量
		$user = $this->auth->getUser();
		$selfNumber = $user['reward_number'];
		
		$isChain = 0;
		if($agent['chain_user_id']) {
			//连锁店的下级
			$isChain = 1;
			$count = Db::name('chainFunds')->where(['agent_id' => $agent['id']])
				->group('type')
				->column('sum(amount) as amount', 'type');
			
			$saleCommission = $count[1] ?? 0;
			$recommendCommission = $count[2] ?? 0;
			$destoryCommission = $count[3] ?? 0;
			
			$saleCommission = $recommendCommission + $saleCommission;
		} else {
			//连锁店的管理者
			$chain = Db::name('agentChain')
				->where(['user_id' => $userId, 'status' => AgentChain::STATUS_NORMAL])
				->find();
			$chain && $isChain = 2;
		}
		
		//未发货提醒数量
		$sendNumber = ShipOrderService::getSendTipsNumberByAgentId($agent['user_id']) ;
		
		
		$this->success('查询成功', [
			'fans' => $fansNumber,
			'order_number' => $agent['order_number'],
			'sale_number' => $agent['sale_number'],
			'order_amount' => $agent['sale_amount'],
			'stock_order_number' => $waitSignNumber, //待签收发货记录
			'promotion' => $promotionNumber,
			'sale_commission' => $saleCommission + $recommendCommission,
			'stock_commission' => $stockCommission,
			'goods_commission' => $destoryCommission,
			'stock' => $sotckNumber,
			'self_number' => $agent['gift_number'],
			'agentInfo' => $agent,
			'is_admin' => $agent['user_id'] == $userId ? 0 : 1,
			'shop' => $shop,
			'warehouse' => $warehouse,
			'is_chain' => $isChain,
			'send_number' => $sendNumber,
		]);
	}
}